package com.hex.liyue.launchdatemodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.activity.BaseActivity;
import com.hex.liyue.commonmodule.config.Constant;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.giftmodule.activity.MyGiftsActivity;
import com.hex.liyue.launchdatemodule.adapter.AdapterPerson;
import com.hex.liyue.launchdatemodule.presenter.ILaunchDetailsView;
import com.hex.liyue.launchdatemodule.presenter.LaunchDetailsPresenter;
import com.hex.liyue.lookingdatemodule.activity.PersonDetailsActivity;
import com.hex.liyue.lookingdatemodule.bean.AppointmentDetailsResultBean;
import com.hex.liyue.messagemodule.activity.ChatActivity;

import java.io.Serializable;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WJ on 2016/6/15.
 * <p/>
 * 我发起约会列表详情
 */
public class LaunchDetailsActivity extends BaseActivity implements ILaunchDetailsView, AdapterPerson.OnItemClickListener {
    @Bind(R.id.rv_person_list)
    YRecycleview rvPersonList;
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.empty_gift)
    LinearLayout empty_gift;

    private LaunchDetailsPresenter launchDetailsPresenter;
    private String appointment_id;
    private AppointmentDetailsResultBean appointmentDetailsResultBeans;

    @Override
    protected void bindPresenter() {
        launchDetailsPresenter = new LaunchDetailsPresenter(this);
    }

    @Override
    protected void initData(Bundle extras) {

        appointment_id = getIntent().getStringExtra("appointment_id");

        launchDetailsPresenter.requestDetails(appointment_id);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_launch_details;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToobarBack.setVisibility(View.VISIBLE);
    }


    @OnClick({R.id.iv_toobar_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
        }
    }

    @Override
    public void setAdapter(AppointmentDetailsResultBean appointmentDetailsResultBean) {
        tvToobarTitle.setText(appointmentDetailsResultBean.data.location);
        appointmentDetailsResultBeans = appointmentDetailsResultBean;
        AdapterPerson adapterPerson = new AdapterPerson(appointmentDetailsResultBean.data.apply_list, appointmentDetailsResultBean.data.invitations_time);
        adapterPerson.setOnItemClickListener(this);
        rvPersonList.setLayoutManager(new LinearLayoutManager(this));
        rvPersonList.setAdapter(adapterPerson);
        rvPersonList.setEmptyView(empty_gift);
    }

    @Override
    public void showLod(String msg) {
        showLoad(this, msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(this, msg);
    }

    @Override
    public void onYesClickListener(String applyId, String acceptId) {
        launchDetailsPresenter.accept(applyId, acceptId, appointment_id);
    }

    @Override
    public void onNoClickListener(String acceptId) {
        launchDetailsPresenter.reject(acceptId, appointment_id);
    }

    @Override
    public void onLookGiftClickListener() {
        Intent intent = new Intent(this, MyGiftsActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onChatClickListener(String fromUid, String fromName, String fromAvatar) {
        Intent intent = new Intent(this, ChatActivity.class);
        intent.putExtra(Constant.EXTRA_USER_ID, fromUid);
        intent.putExtra("fromAvatar", fromAvatar);
        intent.putExtra("fromName", fromName);
        intent.putExtra("toUid", UserManager.getUserId());
        intent.putExtra("toAvatar", UserManager.getUserAvator());
        intent.putExtra("toName", UserManager.getNickName());
        intent.putExtra("flag", "flag");
        startActivity(intent);
//        Intent intent =new Intent(LaunchDetailsActivity.this, ChatActivity.class);
//        Bundle bundle=new Bundle();
////        bundle.putString("userId",applyId);
////        intent.putExtra("username",applyId);
//        bundle.putString("userId",fromUid);
//        bundle.putString("fromName",fromName);
//        bundle.putString("fromAvatar",fromAvatar);
//        bundle.putString("toAvatar", UserManager.getUserAvator());
//        bundle.putString("toName",UserManager.getNickName());
//        bundle.putString("toUid",UserManager.getUserId());
//        intent.putExtras(bundle);
//        startActivity(intent);
    }

    @Override
    public void onItemClickListener(int position) {
        Intent intent = new Intent(this, PersonDetailsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("uid", appointmentDetailsResultBeans.data.apply_list.get(position).uid);
        bundle.putSerializable("gift_list", (Serializable) appointmentDetailsResultBeans.data.apply_list.get(position).gift_list);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
